package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.InsertteaDao;
import com.zr.model.Teacher;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

import net.sf.json.JSONObject;

public class InsertteaDaoImplljc implements InsertteaDao{

	@Override
	public boolean insertteacher(String tname, String tage, String tsex, String address, String tpnumber,Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer("");
		sql.append(" INSERT INTO teacher(tname,tage,tsex,address,tpnumber)");
		sql.append(" VALUES(?,?,?,?,?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, tname);
			pst.setString(2, tage);
			pst.setString(3, tsex);
			pst.setString(4, address);
			pst.setString(5, tpnumber);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}

	@Override
	public boolean insertUser(String uname, String upsw,int rid,Connection con) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer("");
		sql.append(" INSERT INTO user(uname,upsw,rid)");
		sql.append(" VALUES(?,?,?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, uname);
			pst.setString(2, upsw);
			pst.setInt(3, rid);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}

	@Override
	public Teacher findTidBytnameAndSex(String tname, String tsex) {
		Teacher t = new Teacher();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT teacher.tid FROM teacher");
		sql.append(" where tname=? and tsex=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1,tname);
			pst.setString(2,tsex);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				t.setTid(rs.getInt("tid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return t;
		
	}

	@Override
	public boolean updateTidFromUser(int tid,int uid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer("");
		sql.append(" UPDATE user SET");
		sql.append(" tid=?");
		sql.append(" WHERE uid=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, tid);
			pst.setInt(2, uid);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return flag;
	}

	@Override
	public User findUidByUnameAndUpsw(String uname, String upsw) {
		User u = new User();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT user.uid FROM user");
		sql.append(" where uname=? and upsw=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1,uname);
			pst.setString(2,upsw);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				u.setUid(rs.getInt("uid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}	
}
